<?php

namespace App\Http\Controllers\Merchant;

use App\Coupon;
use App\CouponUse;
use App\Http\Response;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CouponUseController extends Controller
{
    use Response;
    public function index(Request $request)
    {
        $mch_id=auth('merchant')->id();
        $paginate=CouponUse::with(["order","coupon"=>function($query){
            $query->with(["goods"]);
        }])->when($request->get('uid'),function ($query)use ($request){
            $query->where('uid',$request->get('uid'));
        })->when($request->get('status')&&$request->get('status')<3,function ($query)use ($request){
            $query->where('useStatus',$request->get('status')-1);
        })->when($request->get('status')&&$request->get('status')==3,function ($query){
            $query->where('period_time','<',Carbon::now()->toDateTimeString())->where('useStatus',0);
        })->where([['mch_id','=',$mch_id]])->paginate($request->get('pageSize'),'*','pageNo');
        return $this->success(200,$paginate);
    }
}
